const express = require("express");
const ffmpeg = require("fluent-ffmpeg");
const app = express();
const router = require("./router");
app.get("/start-stream", (req, res) => {
  const inputStream =
    "http://192.168.8.1:1985/rtc/v1/whip/?app=live&stream=livestream";
  const outputStream = "rtmp://192.168.8.1/live/livestream";

  ffmpeg(inputStream)
    .inputOptions(["-f", "flv"])
    .videoCodec("libx264")
    .audioCodec("aac")
    .audioFrequency(44100)
    .output(outputStream)
    .on("start", (commandLine) => {
      console.log("FFmpeg process started with command:", commandLine);
    })
    .on("error", (err) => {
      console.error("Error:", err);
      res.status(500).send("Error starting stream");
    })
    .on("end", () => {
      console.log("FFmpeg process finished");
    })
    .run();

  res.send("Stream started");
});

app.get("/stop-stream", (req, res) => {
  if (ffmpegProcess) {
    ffmpegProcess.kill("SIGINT");
    res.send("Stream stopped");
  } else {
    res.send("No active stream to stop");
  }
});
app.use("", router),
  app.listen(8989, () => {
    console.log("http://localhost:8989");
  });
